$(function () {
     function getCookie(cookie_name){
           var allcookies = document.cookie;
           var cookie_pos = allcookies.indexOf(cookie_name);
           if (cookie_pos != -1){
               cookie_pos += cookie_name.length + 1;
           var cookie_end = allcookies.indexOf(";", cookie_pos);
           if (cookie_end == -1){cookie_end = allcookies.length;}
           var value = unescape(allcookies.substring(cookie_pos, cookie_end));}
           return value;
           }
          var cookie_val = getCookie("token");
      $.ajax({
      	 type: "POST",
      	 url:  "/OA/staff/query",
      	 dataType:"json",
         data:{id:cookie_val},
      	 success: function(response){
      	 	if (response.ok==true&&cookie_val==response.data.id) {
      	 		$("#nowUsername").html(response.data.loginname);
      	 	}else if(response.ok==false){
      	 		location.href = "/login.html";
      	 	}
      	 }
      })

    $('#logout').click(function () {
        $.ajax({
            type: "POST",
            url:  "/OA/logInOut/logout",
            dataType : 'json',
            success: function (response) {
                if (response.ok == true) {
                    console.log(response)
                    function delCookie($name){
                       var myDate=new Date();
                           myDate.setTime(-1000);//设置时间
                            document.cookie=$name+"=''; expires="+myDate.toGMTString();
                         }
                    delCookie("token")
                    location.href = "/login.html";
                    return true;
                }
            }
        })
    })
// 小铃铛滚动切换
    $(".font_inner li:eq(0)").clone(true).appendTo($(".font_inner"));//克隆第一个放到最后(实现无缝滚动)
    var liHeight = $(".swiper_wrap").height();//一个li的高度
    //获取li的总高度再减去一个li的高度(再减一个Li是因为克隆了多出了一个Li的高度)
    var totalHeight = ($(".font_inner li").length *  $(".font_inner li").eq(0).height()) -liHeight;
    $(".font_inner").height(totalHeight);//给ul赋值高度
    var index = 0;
    var autoTimer = 0;//全局变量目的实现左右点击同步
    var clickEndFlag = true; //设置每张走完才能再点击

    function tab(){
        $(".font_inner").stop().animate({
            top: -index * liHeight
        },400,function(){
            clickEndFlag = true;//图片走完才会true
            if(index == $(".font_inner li").length -1) {
                $(".font_inner").css({top:0});
                index = 0;
            }
        })
    }

    function next() {
        index++;
        if(index > $(".font_inner li").length - 1) {//判断index为最后一个Li时index为0
            index = 0;
        }
        tab();
    }
    function prev() {
        index--;
        if(index < 0) {
            index = $(".font_inner li").size() - 2;//因为index的0 == 第一个Li，减二是因为一开始就克隆了一个LI在尾部也就是多出了一个Li，减二也就是_index = Li的长度减二
            $(".font_inner").css("top",- ($(".font_inner li").size() -1) * liHeight);//当_index为-1时执行这条，也就是走到li的最后一个
        }
        tab();
    }
    //切换到下一张
    $(".swiper_wrap .gt").on("click",function() {
        if(clickEndFlag) {
            next();
            clickEndFlag = false;
        }
    });
    //切换到上一张
    $(".swiper_wrap .lt").on("click",function() {
        if(clickEndFlag) {
            prev();
            clickEndFlag = false;
        }
    });
    //自动轮播
    autoTimer = setInterval(next,3000);
    $(".font_inner a").hover(function(){
        clearInterval(autoTimer);
    },function() {
        autoTimer = setInterval(next,3000);
    })

    //鼠标放到左右方向时关闭定时器
    $(".swiper_wrap .lt,.swiper_wrap .gt").hover(function(){
        clearInterval(autoTimer);
    },function(){
        autoTimer = setInterval(next,3000);
    })
    //1文字轮播(2-5页中间)结束
    // 给每个滚动条加数字;
    $.ajax({
        type: "GET",
        url:  "/OA/enrolone2one/approveNumber",
        dataType : 'json',
        success: function (response) {
            if (response.ok == true) {
               $("#number_1").html(response.data)
            }
        }
    })
    $.ajax({
        type: "GET",
        url:  "/OA/dropone2one/approveNumber",
        dataType : 'json',
        success: function (response) {
            if (response.ok == true) {
                $("#number_2").html(response.data)
            }
        }
    })
    $.ajax({
        type: "GET",
        url:  "/OA/mend/approveNumber",
        dataType : 'json',
        success: function (response) {
            if (response.ok == true) {
                $("#number_3").html(response.data)
            }
        }
    })
    $.ajax({
        type: "GET",
        url:  "/OA/enrolclazz/approveNumber",
        dataType : 'json',
        success: function (response) {
            console.log(response)
            if (response.ok == true) {
                $("#number_4").html(response.data)
            }
        }
    })
    $.ajax({
        type: "GET",
        url:  "/OA/dropclazz/approveNumber",
        dataType : 'json',
        success: function (response) {
            console.log(response)
            if (response.ok == true) {
                $("#number_5").html(response.data)
            }
        }
    })
    $.ajax({
        type: "GET",
        url:  "/OA/mend/approveNumber?type=2",
        dataType : 'json',
        success: function (response) {
            console.log(response)
            if (response.ok == true) {
                $("#number_6").html(response.data)
            }
        }
    })
    // 时间
    function clock_12h() {
        var today = new Date(); //获得当前时间
        //获得年、月、日，Date()函数中的月份是从0－11计算
        var year = today.getFullYear();
        var month = today.getMonth()+1;
        var date = today.getDate();
        var hour = today.getHours();  //获得小时、分钟、秒
        var minute = today.getMinutes();
        var second = today.getSeconds();
        var apm="AM"; //默认显示上午: AM
        if (hour>12) //按12小时制显示
        {
            hour=hour-12;
            apm="PM"  ;
        }
        var weekday = 0;
        switch(today.getDay()){
            case 0:
                weekday = "星期日";
                break;
            case 1:
                weekday = "星期一";
                break;
            case 2:
                weekday = "星期二";
                break;
            case 3:
                weekday = "星期三";
                break;
            case 4:
                weekday = "星期四";
                break;
            case 5:
                weekday = "星期五";
                break;
            case 6:
                weekday = "星期六";
                break;
        }
        /*设置div的内容为当前时间*/
        document.getElementById("myclock").innerHTML="<h2 style='font-size: 14px;display: inline-block'>你好，职卓OA欢迎您！</h2><h2 style='font-size: 14px;display: inline-block'>"+year+"年"+month+"月"+date+"日&nbsp;"+hour+":"+minute+":"+second+"&nbsp;"+apm+"&nbsp;"+weekday+"</h2>";
    }
    clock_12h()
    setInterval(function () {
        clock_12h()
    },1000)
})
// 首页导航条
app.controller("indexController",function($scope, $http){
    $http({
        method: 'GET',
        url: '/OA/staff/roles',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    }).success(function (response) {
        $scope.roles=[];
        $scope.roles=response.data;
        function permissions(jobs){
            var rs=false;
            for (var i = 0; i < $scope.roles.length; i++) {
                if ($scope.roles[i] == jobs) {
                    rs = true;
                }else{
                    rs= false;
                }
            }
            return rs
        }
        $scope.admin=permissions(7);
        $scope.ROLE_CONSULTING=permissions(1);
        $scope.ROLE_CONSULTING_COMPETENT=permissions(2);
        $scope.ROLE_EDUCATION=permissions(3);
        $scope.ROLE_EDUCATION_COMPETENT=permissions(4);
        $scope.ROLE_PRINCIPAL=permissions(5);
        $scope.ROLE_DIRECTOR=permissions(6);
        $scope.ROLE_TEACHER=permissions(8);
        $scope.ROLE_TEACHER_HEAD=permissions(9);
        $scope.ROLE_TEACHER_COMMETENT=permissions(10);
        $scope.ROLE_TEACHER_DIRECTOR=permissions(11);
        $scope.ROLE_FINANCE=permissions(12);
        $scope.ROLE_PERSONNEL=permissions(13);
    })
});

 
